package com.zhangzhibo.controller.bill;

import com.zhangzhibo.pojo.Bill;
import com.zhangzhibo.pojo.User;
import com.zhangzhibo.service.bill.BillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.*;

@Controller
public class BillController {
    @Autowired
    @Qualifier("billServiceImpl")
    BillService billService;


    @RequestMapping("/user/billDo/billList")
    public String billPage(){
        return "billlist";
    }
    @RequestMapping("/user/billDo/billQuery")
    @ResponseBody
    public Map<String, Object> getBillList(String queryProductName, int queryProviderId, int queryIsPayment, int currentPageNo, int pageSize){
        Map<String, Object> map = new HashMap<>();
        int totalNum = billService.getBillCount(queryProductName, queryProviderId, queryIsPayment);
        int totalPage = (totalNum/pageSize);
        if(totalPage < (float) totalNum/pageSize){
            totalPage = totalPage+1;
        }
        List<Bill> billList = billService.getBillList(queryProductName, queryProviderId, queryIsPayment, currentPageNo, pageSize);
        map.put("totalNum",totalNum);
        map.put("totalPage",totalPage);
        map.put("billList",billList);
        return map;
    }


    //订单删除
    @RequestMapping("/user/billDo/delete")
    public String deleteBill(Integer id){
        billService.deleteBill(id);
        return "forward:/user/billDo/billQuery";
    }


    //订单信息查看
    @RequestMapping("/user/billDo/check")
    @ResponseBody
    public Bill checkBill(Integer id){
        Bill BillById = billService.findByBillId(id);
        return  BillById;
    }


    //订单信息修改
    @RequestMapping("/user/billDo/modifyPage")
    public String modifyPage(Model model, Integer id){
        Bill bill = billService.findByBillId(id);
        model.addAttribute("bill",bill);
        return "modifybill";
    }
    @RequestMapping("/user/billDo/modify")
    public String modifyBill(HttpServletRequest request, HttpServletResponse response){
        Bill bill = new Bill();
        User user = (User)request.getSession().getAttribute("userSession");
        bill.setId(Integer.parseInt(request.getParameter("id")));
        bill.setModifyBy(user.getId());
        bill.setModifyDate(new Date());
        ModifyAndAddBill(bill,request,response);
        billService.modifyBill(bill);
        return "redirect:/user/billDo/billList";
    }


    /**
     * 订单编码问题，主键连续自增问题
      * @return
     */
    //订单信息添加
    @RequestMapping("/user/billDo/addBillPage")
    public String addBillPage(Model model){
        //设置订单编码BILL0000(年)_当前编码位数字最大值+1
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());//放入Date类型数据
        int year = calendar.get(Calendar.YEAR);
        //checkBillCodeNumber()获取当前最大id值
        int i = billService.checkBillCodeNumber() + 1;
        String billCode = "BILL"+year+"_"+i;
        model.addAttribute("billCode", billCode);
        return "addbill";
    }

    //添加订单
    @RequestMapping("/user/billDo/addBill")
    public String addBillPage(HttpServletRequest request,HttpServletResponse response){
        Bill bill = new Bill();
        User user = (User)request.getSession().getAttribute("userSession");
        bill.setCreatedBy(user.getId());
        bill.setCreationDate(new Date());
        ModifyAndAddBill(bill,request,response);
        billService.addBill(bill);
        return "redirect:/user/billDo/billList";
    }


    public void ModifyAndAddBill(Bill bill,HttpServletRequest request,HttpServletResponse response){
        bill.setBillCode(request.getParameter("billCode"));
        bill.setProductName(request.getParameter("productName"));
        bill.setProductUnit(request.getParameter("productUnit"));
        bill.setProductCount(new BigDecimal(request.getParameter("productCount")));
        bill.setTotalPrice(new BigDecimal(request.getParameter("totalPrice")));
        bill.setProviderId(Integer.parseInt(request.getParameter("proNameSelected")));
        bill.setIsPayment(Integer.parseInt(request.getParameter("isPayment")));
    }

}
